package com.kermel.andromeda.data;

import com.kermel.common.util.StringUtils;

public class TimeRemaining {
    private int hours;
    private int minutes;
    private int seconds;
    
    public TimeRemaining(int hours, int minutes) {
        this(hours, minutes, 0);
        
        if (hours == 0 && minutes == 0 && seconds == 0) {
            this.seconds = 59;
        }
    }

    // Used by unit test
    TimeRemaining(int hours, int minutes, int seconds) {
        this.hours = hours;
        this.minutes = minutes;
        this.seconds = seconds;
    }
    
    public void decrementOneSecond() {
        seconds--;
        
        if (seconds < 0) {
            
            decrementOneMinute();
            
            if (minutes < 0) {
                minutes = 0;
                seconds = 0;
            } else {
                seconds = 59;
            }
        }
    }
    
    private void decrementOneMinute() {
        minutes--;
        
        if (minutes < 0) {
            
            decrementOneHour();
            
            if (hours < 0)
                hours = 0;
            else
                minutes = 59;
        }
    }
    
    private void decrementOneHour() {
        hours--;
    }
    
    public String toFormattedString() {
        String result = StringUtils.EMPTY;
        
        if (hours > 0)
            result = String.format("%02d:%02d:%02d", hours, minutes, seconds);
        else
            result = toMinutesAndSecondsString();
        
        return result;
    }
    
    private String toMinutesAndSecondsString() {
        String result = String.format("%02d:%02d", minutes, seconds);
        return result;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        
        sb.append(String.format("  hours: %d\n", hours));
        sb.append(String.format("minutes: %d\n", minutes));
        sb.append(String.format("seconds: %d\n", seconds));
        
        return sb.toString();
    }
    
    public int getHours() {
        return hours;
    }

    public int getMinutes() {
        return minutes;
    }

    public int getSeconds() {
        return seconds;
    }

}
